﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.Client;

namespace TFhisto
{
    /// <summary>
    /// Used to construct the hyperlinks shown on the nodes
    /// </summary>
    internal static class UrlHelper
    {
        /// <summary>
        /// Performs a replace on the team project collection Uri of http or https with mtm
        /// </summary>
        /// <param name="wis">The work item store</param>
        /// <returns>A formatted Url <example>mtm://[server]:[port]/tfs/[collection]</example> for use by MTM</returns>
        internal static string GetBaseMtmUrl(WorkItemStore wis)
        {
            string server = string.Empty;

            if (wis.TeamProjectCollection.Uri.Scheme.ToLowerInvariant() == "http")
            {
                server = wis.TeamProjectCollection.Uri.ToString().Replace("http", "mtm");  //Returns http://[server]:[port]/tfs/[collection] - need to replace http with mtm
            }
            else if (wis.TeamProjectCollection.Uri.Scheme.ToLowerInvariant() == "https")
            {
                server = wis.TeamProjectCollection.Uri.ToString().Replace("https", "mtm");  //Returns http://[server]:[port]/tfs/[collection] - need to replace https with mtm
            }

            return server;
        }

        /// <summary>
        /// Returns the Url for the work item in MTM
        /// </summary>
        /// <param name="wis">The Work Item Store</param>
        /// <param name="workItemId">The ID of the work item</param>
        /// <param name="projectName">The name of the project</param>
        /// <returns></returns>
        internal static string GetMtmWorkItemUrl(WorkItemStore wis, int workItemId, string projectName)
        {
            string server = GetBaseMtmUrl(wis);
            string url = server + string.Format("/p:{0}/testing/testcase/open?id={1}", projectName, workItemId);
            return url;
        }

        /// <summary>
        /// Returns the Url for the plan in MTM
        /// </summary>
        /// <param name="wis">The Work Item Store</param>
        /// <param name="planId">The ID of the plan</param>
        /// <param name="projectName">The name of the project</param>
        /// <returns></returns>
        internal static string GetMtmTestPlanUrl(WorkItemStore wis, int planId, string projectName)
        {
            string server = GetBaseMtmUrl(wis);
            string url = server + string.Format("/p:{0}/testing/testplan/connect?id={1}", projectName, planId);
            return url;
        }

        /// <summary>
        /// Returns the Url for the test run in MTM
        /// </summary>
        /// <param name="wis">The Work Item Store</param>
        /// <param name="runId">The ID of the test run</param>
        /// <param name="projectName">The name of the project</param>
        /// <returns></returns>
        internal static string GetMtmTestRunUrl(WorkItemStore wis, int runId, string projectName)
        {
            string server = GetBaseMtmUrl(wis);
            string url = server + string.Format("/p:{0}/testing/testrun/open?id={1}", projectName, runId);
            return url;
        }

        /// <summary>
        /// Returns the Url for the test result in MTM
        /// </summary>
        /// <param name="wis">The Work Item Store</param>
        /// <param name="runRelativeId">The ID of the test run</param>
        /// <param name="resultId">The ID of the test result</param>
        /// <param name="projectName">The name of the project</param>
        /// <returns></returns>
        internal static string GetMtmTestResultsUrl(WorkItemStore wis, int runRelativeId, int resultId, string projectName)
        {
            string server = GetBaseMtmUrl(wis);
            string url = server + 
                string.Format("/p:{0}/testing/testresult/open?id={1}&runid={2}",
                projectName, resultId, runRelativeId);
            return url;
        }

        /// <summary>
        /// Returns the Team Web Access link for the work item
        /// </summary>
        /// <param name="wis">The work item store</param>
        /// <param name="workItemId">The ID of the work item to retrieve the URL for</param>
        /// <returns>The Uri to the work item</returns>
        internal static Uri GetTWAWorkItemUrl(WorkItemStore wis, int workItemId)
        {
            TswaClientHyperlinkService hyperlinkService = wis.TeamProjectCollection.GetService<TswaClientHyperlinkService>();
            return hyperlinkService.GetWorkItemEditorUrl(workItemId);
        }

        /// <summary>
        /// Returns the Team Web Access url for the changeset details
        /// </summary>
        /// <param name="wis">The work item store</param>
        /// <param name="changesetId">The ID of the changeset to return the URL for</param>
        /// <returns>The Uri of the changeset</returns>
        internal static Uri GetTWAChangesetUrl(WorkItemStore wis, int changesetId)
        {
            TswaClientHyperlinkService hyperlinkService = wis.TeamProjectCollection.GetService<TswaClientHyperlinkService>();
            return hyperlinkService.GetChangesetDetailsUrl(changesetId);
        }

        /// <summary>
        /// Returns the URL for the changeset link including a placeholder for the changeset ID
        /// </summary>
        /// <param name="wis">The work item store</param>
        /// <returns>A url to a changeset - but not an actual changeset - the changeset ID is a placeholder</returns>
        internal static string GetTWAChangesetBaseUrl(WorkItemStore wis)
        {
            string baseUri = GetTWABaseUrl(wis);
            baseUri += "/UI/Pages/Scc/ViewChangeset.aspx?changeset={0}";
            return baseUri;
        }

        internal static string GetTWAChangeBaseUrl(WorkItemStore wis)
        {
            string baseUri = GetTWABaseUrl(wis);
            baseUri += "/UI/Pages/Scc/ViewSource.aspx?path={0}";
            return baseUri;
        }

        internal static string GetTWACompareChangeBaseUrl(WorkItemStore wis)
        {
            string baseUri = GetTWABaseUrl(wis);
            baseUri += "/UI/Pages/Scc/Compare.aspx?path={0}";
            return baseUri;
        }

        /// <summary>
        /// Returns the Url for a work item in TWA
        /// </summary>
        /// <param name="wis">The work item store</param>
        /// <returns>A placeholder Url to a work item details page</returns>
        internal static string GetTWAWorkItemBaseUrl(WorkItemStore wis)
        {
            string baseUri = GetTWABaseUrl(wis);
            baseUri += "/UI/Pages/WorkItems/WorkItemEdit.aspx?id={0}";
            return baseUri;
        }

        /// <summary>
        /// Returns the base Uri for Team Web Access
        /// </summary>
        /// <param name="wis">The work item store</param>
        /// <returns>The base Uri for TWA <example>http://[server]:[port]/tfs/web</example></returns>
        private static string GetTWABaseUrl(WorkItemStore wis)
        {
            TswaClientHyperlinkService hyperlinkService = wis.TeamProjectCollection.GetService<TswaClientHyperlinkService>();
            return hyperlinkService.GetHomeUrl().ToString();
        }
    }
}